package com.cmc.cloud.cmclink.doc.util;

import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.cmc.cloud.cmclink.doc.util.respvo.CommodityEnAiCheckResp;

import java.util.HashMap;
import java.util.Map;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

/**
 * @author 周贤
 * @date 2025/11/12
 */
public class HttpClientUtil {

  private static final Gson gson = new Gson();

  private static final String url = "http://aipaas-ais.cmft.com/Others/snl-reasons-v1";

    /**
     * 发送GET请求
     */
    public static String sendGet(String url, Map<String, String> headers) {
        StringBuilder result = new StringBuilder();
        try {
            URL obj = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) obj.openConnection();
            connection.setRequestMethod("GET");

            // 设置请求头
            if (headers != null) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    connection.setRequestProperty(entry.getKey(), entry.getValue());
                }
            }

            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(
                        new InputStreamReader(connection.getInputStream()));
                String inputLine;

                while ((inputLine = in.readLine()) != null) {
                    result.append(inputLine);
                }
                in.close();
            } else {
                System.out.println("GET request failed. Response Code: " + responseCode);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.toString();
    }

    /**
     * 发送POST请求
     */
    public static <T> T sendPost(String url, Map<String, Object> paramMap, Map<String, String> headers,Class<T> clazz) {
        StringBuilder result = new StringBuilder();
        try {
            URL obj = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) obj.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);

            // 设置请求头
            if (headers != null) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    connection.setRequestProperty(entry.getKey(), entry.getValue());
                }
            }

            // 发送请求体
            String jsonBody = gson.toJson(paramMap);
            if (jsonBody != null && !jsonBody.isEmpty()) {
                try (OutputStream os = connection.getOutputStream()) {
                    byte[] input = jsonBody.getBytes(StandardCharsets.UTF_8);
                    os.write(input, 0, input.length);
                }
            }

            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK ||
                    responseCode == HttpURLConnection.HTTP_CREATED) {
                BufferedReader in = new BufferedReader(
                        new InputStreamReader(connection.getInputStream()));
                String inputLine;

                while ((inputLine = in.readLine()) != null) {
                    result.append(inputLine);
                }
                in.close();
            } else {
                System.out.println("POST request failed. Response Code: " + responseCode);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            return gson.fromJson(result.toString(), clazz);
        } catch (Exception e) {
            throw new RuntimeException("JSON 转换失败: " + e.getMessage(), e);
        }
    }

    // 使用示例
    public static void main(String[] args) {

        // POST请求示例
        Map<String, Object> map = new HashMap<>(16);
        map.put("text","危险品描述");
        map.put("top_num","1");

        CommodityEnAiCheckResp commodityEnAiCheckResp = sendPost(url, map, null, CommodityEnAiCheckResp.class);
        System.out.println("POST Response: " + commodityEnAiCheckResp);
    }
}
